[PHP] Making a good singleton registry class structure which hold your objects

Posted by Saif Bechan on Stack Overflow See other posts from Stack Overflow or by Saif Bechan
Published on 2010-03-08T13:01:01Z Indexed on 2010/03/08 13:06 UTC
Read the original article Hit count: 870

I am working on a web application in PHP. I have a singleton class called registry. This class will hold all the objects i need throughout my application, such as loader classes, template classes, database, classes, etc.

When an object of the registry class is created I send it an array with the classes it need to load:

// Create the registry
$registry = registry::singleton();

// Store those core objects
$registry->storeObjects(Array('session','db','page','template','errors'));

In this example I only put some of the classes, to get the basic idea.

Now I have some classes in the registry that use each other. For example the 'errors' object uses the 'page' object.

Now I was wondering if it is a good practice to make an instance of the registry object in the errors object. Like this;

class errors{
    private $registry;

    public function __construct(){
        $this->registry = registry::singleton();
    }
}

So there is an instance of the registry object, inside an object of the registry object. This does not sound like a good idea to me. Anyone have a suggestion how to model such a thing?

© Stack Overflow or respective owner

Related posts about php

Related posts about singleton